<?php

class VisitInitAction extends SzAbstractAction
{
    protected $paramTypes = array(
        SzAbstractAction::TYPE_INT,
        SzAbstractAction::TYPE_INT
    );

    /**
     * 访问好友
     *
     * @param int $userId
     * @param int $otherUserId
     * @param int $visitType default 1   1. 好友  2.公会 3.室外市集  4.排行榜 5.登报
     * @return SzResponse
     */
    public function execute($userId, $otherUserId, $visitType = 1)
    {
        if (!GameService::get()->verifySysUnlock($userId, GameConst::FUNC_VISIT_FUNC) || $userId == $otherUserId) {
            return $this->buildResponse(false);
        }
        /**
         * @var ModuleProfileVo $profile
         * @var ModuleProfileInfoVoList $profileInfoList
         * @var GroundVoList $groundList
         * @var BuildingVoList $buildingList
         * @var OrderBoardVoList $orderBoardList
         * @var OrderVoList $orderList
         * @var PlantVoList $plantList
         * @var GoodsVoList $goodsList
         * @var MineVoList $mineList
         * @var ModuleProfileVo $profileInfo
         * @var CultivateLandVoList $cultivateLandList
         * @var PlantSeedVoList $seedList
         */
        list(
            $profile,
            $profileInfoList,
            $groundList,
            $buildingList,
            $orderBoardList,
            $orderList,
            $plantList,
            $goodsList,
            $mineList,
            $townName,
            $cultivateLandList,
            $seedList,
            ) = VisitService::get()->init($otherUserId);

        $timezoneOffset = SzUtility::getTimeZoneOffset(SzConfig::get()->loadAppConfig('app', 'TIMEZONE'));

        $totalCount = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'umberOfMutualaid', true);
        $friendCount = FriendService::get()->getFriendHandleInfo($userId, $otherUserId,$totalCount);

        // 增加访问玩家记录
        if (VisitService::get()->addVisitRecord($userId, $otherUserId)) {
            MissionService::get()->progress($userId, GameConst::ENTITY_ACTION_VISIT_FRIEND, GameConst::ENTITY_TYPE_NULL, 99999999, 1);
        }

        // 记录日志：好友访问其他玩家
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:friends|action:visit|',
                'item' => $otherUserId,
                'value' => 1,
            )
        ));

        $diff = 0;
        if (SzConfig::get()->loadAppConfig('app', 'GM_OPEN')) {
            $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
            $diff = $redisDb->hGet(GameConst::CACHE_KEY_DIFF_TIME, $userId);
            if (!$diff) {
                $diff = 0;
            }
        }

        $headFrame = 0;
        $profileInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_HEAD_FRAME,false);
        if ($profileInfo){
            $headFrame = $profileInfo->getInfo();
        }

        //添加鲜花升星标记0没有三星，1三星特效
        $flowerData = $plantList->toArray();
        $resFlowers = array();
        //查询鲜花升星数据
        /**
         * @var $flowerStarList FlowerStarInfoVoList
         * @var $flowerStarInfo FlowerStarInfoVo
         */
        $flowerStarList = SzPersister::get()->getVoList($otherUserId, GameConst::ORM_NAME_FLOWER_STAR);
        foreach ($flowerData as $key=>$data){
            $starFlag = 0;
            $resFlowers[$key] = $data;
            $flowerStarInfo = $flowerStarList->getElement($data['seedId'], false);
            if ($flowerStarInfo){
                if (explode(':',json_decode($flowerStarInfo->getFirst()))[0] == 1 &&
                    explode(':',json_decode($flowerStarInfo->getSecond()))[0] == 1 &&
                    explode(':',json_decode($flowerStarInfo->getThird()))[0] == 1)
                {
                    $starFlag = 1;
                }
            }

            $resFlowers[$key]['starFlag'] = $starFlag;
        }

        return $this->buildResponse(array(
            'profile' => $profile->toArray(),
            'profileInfoList' => $profileInfoList->toArray(true),
            'groundList' => $groundList->toArray(),
            'buildingList' => $buildingList->toArray(),
            'orderBoardList' => $orderBoardList->toArray(),
            'orderList' => $orderList->toArray(),
            'plantList' => $resFlowers,
            'goodsList' => $goodsList->toArray(),
            'mineList' => $mineList->toArray(),
            'timezoneOffset' => $timezoneOffset,
            'townName' => $townName,
            'cultivateLandList' => $cultivateLandList->toArray(),
            'seedList' => $seedList->toArray(),
            'diff' => $diff,
            'friendCount' => $friendCount,
            'treeInfo' => TreeService::get()->info($otherUserId),
            'headFrame' => $headFrame
        ));
    }
}